$(document).ready(function() {


    $.fn.extend({
        comment:function(){
            $(this).click(function(){
                comment_btn($(this));
            });
        }
    });
    $("#comment_btn").comment();


    var $input_name ="Comment[content]";
    var max_content_lenght =1000;
    $.fn.extend({
        math_length:function(){
            $(this).keyup(function(){
                var len = max_content_lenght - $("textarea[name='"+$input_name+"']").val().length;
                $("#num").html(len);
                $("#comment-length-tip").html(len+"/"+max_content_lenght);
            });
        }
    })
    $("textarea[name='"+$input_name+"']").math_length();

    $.fn.extend({
        do_top:function(){
            $(this).click(function(){
//             jQuery.dotop($(this));
                jQuery.dotop($(this));
            });
        }
    });
    $(".do_top").do_top();
    $.fn.extend({
        do_step:function(){
            $(this).click(function(){
                jQuery.dostep($(this));
            });
        }
    });
    $(".do_step").do_step();

    $('#comment_verify_image').click(
        function(){
            $('#comment_verify_image').attr('src',baseurl +'comment/verify_image?'+ Math.random());
            $('#comment_verify').focus();
            return false;
        }
    );

});
(function($){

    $.fn.slide=function(options){
        var form_comment = $('#form_comment');
        // alert("111");
//                           form_comment.attr('action' , "index.php?do=comment&action=insert_comment");
        var url = form_comment.attr('action');
        var content = $("#content").val();
        var id = form_comment.find("input[name='id']").val();
        var type = form_comment.find("input[name='type']").val();
        var verify = form_comment.find("input[name='comment_verify']").val();
        if( content.length > 255){ ship_mess('评论内容不能超过255');return false;}
        if(url){
            if(content && verify && type){
                $.ajax({
                    url: baseurl +'/comment/docomment',
                    type: 'POST',
                    data:{id:id,content:content,type:type,verify:verify},
                    dataType: 'json',
                    timeout: 10000,
                    error: function(){alert('Error Comment');},
                    success: function(data){
                        if(data.stats =='-1'){
                            var mess = '不支持匿名评论,请先登录！';
                            //                                            location.href='logging.php';
                        }else if(data.stats =='1'){
                            var mess = '评论成功！';
                            form_comment.find("input[name='verify']").val('');
                            form_comment.find('textarea').val('');
                            $('#imgcaptcha').attr('src','captcha.php?sid=' + Math.random());
                        }else{
                            var mess = data.mess;
                        }
                        ship_mess(mess);
                    }
                });
            }else{
                ship_mess('您没有填写内容或者验证码！');return false;
            }
        }
    };
    $.extend({
        dotop:function(obj){
            var $id = obj.parent().attr('data-id');
            var $type = obj.parent().attr('data-model');
            var $what = 'top';
            $.ajax({
                url: baseurl +'comment/docomment',
                type: 'POST',
                data:{id:$id,what:$what,type:$type},
                dataType: 'json',
                timeout: 10000,
                error: function(){alert('Error Comment');},
                success: function(data){
                    if(data.stats =='1'){
                        var mess = '评论成功！';
                        obj.find('em').html(parseInt(obj.find('em').html())+parseInt(1));
                    }else{
                        var mess = data.mess;
                    }
                    ship_mess(mess);
                }
            });
        },
        dostep:function(obj){
            var $id = obj.parent().attr('data-id');
            var $type = obj.parent().attr('data-model');
            var $what = 'step';
            $.ajax({
                url: baseurl +'/comment/docomment',
                type: 'POST',
                data:{id:$id,what:$what,type:$type},
                dataType: 'json',
                timeout: 10000,
                error: function(){alert('Error Comment');},
                success: function(data){
                    if(data.stats =='1'){
                        var mess = '评论成功！';
                        obj.find('em').html(parseInt(obj.find('em').html())+parseInt(1));
                    }else{
                        var mess = data.mess;
                    }
                    ship_mess(mess);
                }
            });
        },
    });

})(jQuery);

function comment_btn(obj){
    jQuery("#comment_btn").slide(obj);
}











jQuery(document).ready(function ($) {
    $("img").lazyload();

    // $("img").lazyload({
    //     placeholder: "http://jileiba.com/wp-includes/images/loading03.gif",
    //     effect: "fadeIn"
    // });

});
$(document).ready(function(){



    $('#verify_image').click(
            function(){
            $('#verify_image').attr('src',baseurl +'/member/verify_image?'+ Math.random());
            $('#verify').focus();
            return false;
            }
    );
    $('#comment_verify_image').click(
            function(){
            $('#comment_verify_image').attr('src',baseurl +'/comment/verify_image?'+ Math.random());
            $('#comment_verify').focus();
            return false;
            }
    );



});



// window.onload = function() {
//     window.onscroll = function() {
//         var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
//         var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
//         var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
//         if (scrollTop + windowHeight >= scrollHeight - 350) { //考虑到滚动的位置一般可能会大于一点可滚动的高度，所以这里不能用等于
//             let div = document.getElementsByClassName("wlj-sider-wrapper")[0]
//             //  		console.log("距顶部" + scrollTop + "可视区高度" + windowHeight + "滚动条总高度" + scrollHeight);
//             // console.log(div,div.style)
//             // div.style.display = "none"
//             div.style.position = "absolute"
//         } else {
//             let div = document.getElementsByClassName("wlj-sider-wrapper")[0]
//             // div.style.display = "block"
//             div.style.position = "relative"
//         }
//     }
//
// }

function toTopInit() {
//	var pageHeight = document.documentElement.clientHeight || document.body.clientHeight,
//	scrollTop = document.documentElement.scrollTop || document.body.scrollTop,
//	offsetHeight = document.body.offsetHeight,
//	$anchorObj = $("#anchor"),
//	footTop = $(".foot").offset().top;
//	if (navigator.userAgent.indexOf("MSIE 6.0") > 0){
//		if ((pageHeight + scrollTop) >= footTop) {
//			$anchorObj.css("top", footTop - $anchorObj.outerHeight(true));
//		} else {
//			$anchorObj.css("top", pageHeight + scrollTop - $anchorObj.outerHeight(true) - 40);
//		}
//	} else {
//		if ((pageHeight + scrollTop) >= footTop + 40) {
//			$anchorObj.css("bottom", pageHeight + scrollTop - footTop + "px");
//		} else {
//			$anchorObj.css("bottom", "40px");
//		}
//	}
//	scrollTop > 0 ? $("#toTop").slideDown(250) : $("#toTop").slideUp(250);
//	return false;
}


function hitadd(id) {
    var url=  baseurl +'/ajax/hitadd?'+ Math.random();
    var model = 'page';
    $.ajax({
        type: "post",
        url: url,
        data: {id: id, model: model},
        dataType: 'json'
    });


        //     $.ajax({
        //         url:"<?=Url::to(['article/view-ajax'])?>",
        //         data:{id:<?=$model->id?>},
        // success:function (data) {
        //     $("span.count").html(data.likeCount);
        //     $("span#scanCount").html(data.scanCount);
        //     $("span#commentCount").html(data.commentCount);
        // }
        // });
}





function getSmsCode() {
   var url=  baseurl +'/member/verify_image?'+ Math.random()
    $.ajax({
        type: "post",
        url:baseurl+'member/ajax_reg',
        data:{agree:agree,username:username,type:type,password:password,repassword:repassword,email:email,verify:verify},
        dataType:'json',
        beforeSend: function(){
            ship_mess('注册中........');
        },
        success: function(data){
            if(data.state === 1){
                ship_mess(data.mess);
                location.href = baseurl;
            }else{
                ship_mess(data.mess);
            }
        },
        error: function(){
            ship_mess(data.mess);
        }
    });
     this.$http
        .post(smsCode, {
            mobile: this.userInfo.mobile,
            usage:"login"
        })
        .then(r => {
            if (r.data) {
                this.$mHelper.toast(`验证码发送成功, 验证码是${r.data}`);
            } else {
                this.$mHelper.toast('验证码已发送.');
            }
            this.smsCodeBtnDisabled = true;
            uni.setStorageSync('loginSmsCodeTime', moment().valueOf() / 1000);
            this.handleSmsCodeTime(59);
        });
}
function handleSmsCodeTime(time) {
    let timer = setInterval(() => {
        if (time === 0) {
            clearInterval(timer);
            this.smsCodeBtnDisabled = false;
        } else {
            this.codeSeconds = time;
            this.smsCodeBtnDisabled = true;
            time--;
        }
    }, 1000);
}




function ship_mess($mess){
    var time = arguments[1]?arguments[1]:3000  ;//默认3秒
    var top  = arguments[2]?arguments[2]:50  ;//默认50%的高度
    $("#ship_mess").remove();
    $(document.body).append('<div id="ship_mess" style="background:#e6e6e6;color:#910a2b;padding:10px 20px;border:1px solid #910a2b;position:fixed;left:45%;top:'+top+'%;">'+$mess+'</div>');
    $("#ship_mess").fadeOut(time); 
    setTimeout('$("#ship_mess").remove()',time);
}  

